//
//  YWMeterSelectMonthCell.m
//  maintain
//
//  Created by Kyo on 16/8/20.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMeterSelectMonthCell.h"
#import "Masonry.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "NSDate+YYAdd.h"
#import "YWPopView.h"
#import "YWCommonTool.h"

@interface YWMeterSelectMonthCell()<YWPopViewDelegate>
@property (nonatomic, strong) UILabel * writeRecordMonthLabel;
@property (nonatomic, strong) UIButton * monthButton;
@property (nonatomic, strong) YWPopView* monthPopView;
@property (nonatomic, strong) NSMutableArray* oldestRecordArray;
@end

@implementation YWMeterSelectMonthCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }
    
    return self;
}

- (NSMutableArray*)oldestRecordArray
{
    if (!_oldestRecordArray) {
        _oldestRecordArray = [NSMutableArray array];
        NSMutableArray *monthArrray = [NSMutableArray array];
        NSDate *nowDate = [NSDate date];
        //前2
        NSDate *before1Month = [nowDate dateByAddingMonths:1];
        NSDate *before2Month = [nowDate dateByAddingMonths:2];
        //后2
        NSDate *after1Month = [nowDate dateByAddingMonths:-1];
        NSDate *after2Month = [nowDate dateByAddingMonths:-2];
        [monthArrray addObject:after2Month];
        [monthArrray addObject:after1Month];
        [monthArrray addObject:nowDate];
        [monthArrray addObject:before1Month];
        [monthArrray addObject:before2Month];
        
        [monthArrray enumerateObjectsUsingBlock:^(NSDate*  _Nonnull date, NSUInteger idx, BOOL * _Nonnull stop) {
            int month = (int)date.month;
            if (month>=1 && month<10) {
                NSString *string = [NSString stringWithFormat:@"%ld年0%d月", (long)date.year, month];
                [_oldestRecordArray addObject:string];
            }else{
                NSString *string = [NSString stringWithFormat:@"%ld年%d月", (long)date.year, month];
                [_oldestRecordArray addObject:string];
            }
        }];
    }
    return _oldestRecordArray;
}

- (UILabel *)writeRecordMonthLabel{
    if (!_writeRecordMonthLabel) {
        UILabel * writeRecordMonthLabel = [[UILabel alloc] init];
        writeRecordMonthLabel.font = kContentFont;
        writeRecordMonthLabel.text = @"抄表月份:";
        [self.contentView addSubview:writeRecordMonthLabel];
        _writeRecordMonthLabel = writeRecordMonthLabel;
    }
    return _writeRecordMonthLabel;
}

- (YWPopView*)monthPopView
{
    if (!_monthPopView) {
        YWPopView* monthPopView = [[YWPopView alloc] initMonthWithDays:self.oldestRecordArray];
        monthPopView.delegate = self;
        _monthPopView = monthPopView;
    }
    return _monthPopView;
}


- (UIButton *)monthButton{
    if (!_monthButton) {
        NSDate* nowDate = [NSDate date];
        NSString* nowDateAndYear = [NSString stringWithFormat:@"%ld年%ld月", (long)nowDate.year, (long)nowDate.month];
        UIButton* monthButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [monthButton setTitle:nowDateAndYear forState:UIControlStateNormal];
        [monthButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [monthButton setBackgroundColor:[UIColor whiteColor]];
        monthButton.titleLabel.font = kContentFont;
        monthButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        monthButton.layer.borderWidth = 1.0;
        monthButton.clipsToBounds = YES;
        monthButton.layer.cornerRadius = 4.0;
        UIImage* btnImage = [UIImage imageNamed:@"scan_arrow"];
        btnImage = [btnImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [monthButton setImage:btnImage forState:UIControlStateNormal];
        monthButton.imageView.contentMode = UIViewContentModeCenter;
        monthButton.imageView.clipsToBounds = NO;
        monthButton.imageEdgeInsets = UIEdgeInsetsMake(0, SCREEN_WIDTH - 10 * 2 - 70 - 20, 0, 0);
        monthButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        [monthButton addTarget:self action:@selector(monthButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:monthButton];
        _monthButton = monthButton;
    }
    return _monthButton;
}

- (void)setUpSubviews{
    [self.writeRecordMonthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(5);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.width.mas_equalTo(@70);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
        make.height.mas_equalTo(34);
    }];
    
    [self.monthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(5);
        make.left.equalTo(_writeRecordMonthLabel.mas_right);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.bottom.mas_equalTo(_writeRecordMonthLabel.mas_bottom);
    }];
    
}

#pragma mark YWPopViewDelegate
- (void)popMonthValue:(NSString*)monthValue
{
    NSString* month = [monthValue stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"月"]];
    month = [month stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
    month = [month stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
    [self.monthButton setTitle:monthValue forState:UIControlStateNormal];
    if ([self.delegate respondsToSelector:@selector(transmitButtonTitle:cell:)]) {
        [self.delegate transmitButtonTitle:month cell:self];
    }

}

- (void)monthButtonEvent:(id)sender
{
    [self.viewController.view endEditing:YES];
    if (self.oldestRecordArray.count) {
        [self.monthPopView show];
        
    }else{
        [YWCommonTool showTextHUDWithContent:@"暂无月份可选"];
    }
}

- (UIViewController*)viewController
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
@end
